import 'package:dio/dio.dart';
import 'package:dio_cache_interceptor/dio_cache_interceptor.dart';

import '../../tools/index.dart';
import 'medical_mock_apis.dart';

/// 医疗相关API接口类
/// 提供用户认证、工作台、采购等功能的网络请求接口
class MedicalApis {
  // ========== 用户认证相关接口 ========== //

  /// 用户登录
  /// [username] 用户名
  /// [password] 密码
  /// [isShowLoad] 是否显示加载动画MedicalMockApis
  static Future loginUser({
    required String username,
    required String password,
    bool isShowLoad = true,
  }) async {
    return await MedicalMockApis.loginUser(
      username: username,
      password: password,
    );
  }

  /// 用户注册
  /// [username] 用户名
  /// [password] 密码
  /// [phone] 手机号
  /// [isShowLoad] 是否显示加载动画
  static Future registerUser({
    required String username,
    required String password,
    required String phone,
    bool isShowLoad = true,
  }) async {
    return await MedicalMockApis.registerUser(
      username: username,
      password: password,
      phone: phone,
    );
  }

  /// 获取用户信息
  /// [userId] 用户ID
  /// [isShowLoad] 是否显示加载动画
  static Future getUserInfo({
    required String userId,
    bool isShowLoad = false,
  }) async {
    Map<String, dynamic> data = {
      "user_id": userId,
    };

    return await request(
      path: "/user/info",
      data: data,
      method: Method.get,
      isShowLoad: isShowLoad,
      isShowError: false,
      policy: CachePolicy.forceCache,
    );
  }

  /// 更新用户信息
  /// [userInfo] 用户信息
  /// [isShowLoad] 是否显示加载动画
  static Future updateUserInfo({
    required Map<String, dynamic> userInfo,
    bool isShowLoad = true,
  }) async {
    return await request(
      path: "/user/update",
      data: userInfo,
      method: Method.post,
      isShowLoad: isShowLoad,
      isShowError: true,
      isShowSuccessToast: true,
      policy: CachePolicy.noCache,
    );
  }

  // ========== 工作台相关接口 ========== //

  /// 获取工作台数据
  /// [isShowLoad] 是否显示加载动画
  static Future getWorkbenchData({
    bool isShowLoad = false,
  }) async {
    return await MedicalMockApis.getWorkbenchData();
  }

  /// 获取患者列表
  /// [page] 页码
  /// [keyword] 搜索关键词
  /// [isShowLoad] 是否显示加载动画
  static Future getPatientList({
    int page = 1,
    String? keyword,
    bool isShowLoad = false,
  }) async {
    return await MedicalMockApis.getPatientList(
      page: page,
      keyword: keyword,
    );
  }

  /// 添加患者
  /// [patientInfo] 患者信息
  /// [isShowLoad] 是否显示加载动画
  static Future addPatient({
    required Map<String, dynamic> patientInfo,
    bool isShowLoad = true,
  }) async {
    return await request(
      path: "/patient/add",
      data: patientInfo,
      method: Method.post,
      isShowLoad: isShowLoad,
      isShowError: true,
      isShowSuccessToast: true,
      policy: CachePolicy.noCache,
    );
  }

  /// 获取处方列表
  /// [patientId] 患者ID
  /// [page] 页码
  /// [isShowLoad] 是否显示加载动画
  static Future getPrescriptionList({
    String? patientId,
    int page = 1,
    bool isShowLoad = false,
  }) async {
    return await MedicalMockApis.getPrescriptionList(
      page: page,
      patientId: patientId,
    );
  }

  /// 保存处方
  /// [prescriptionInfo] 处方信息
  /// [isShowLoad] 是否显示加载动画
  static Future savePrescription({
    required Map<String, dynamic> prescriptionInfo,
    bool isShowLoad = true,
  }) async {
    return await request(
      path: "/prescription/save",
      data: prescriptionInfo,
      method: Method.post,
      isShowLoad: isShowLoad,
      isShowError: true,
      isShowSuccessToast: true,
      policy: CachePolicy.noCache,
    );
  }

  // ========== 采购相关接口 ========== //

  /// 获取采购商品列表
  /// [page] 页码
  /// [category] 商品分类
  /// [keyword] 搜索关键词
  /// [isShowLoad] 是否显示加载动画
  static Future getPurchaseItemList({
    int page = 1,
    String? category,
    String? keyword,
    bool isShowLoad = false,
  }) async {
    return await MedicalMockApis.getPurchaseItemList(
      page: page,
      category: category,
      keyword: keyword,
    );
  }

  /// 获取商品详情
  /// [itemId] 商品ID
  /// [isShowLoad] 是否显示加载动画
  static Future getPurchaseItemDetail({
    required String itemId,
    bool isShowLoad = true,
  }) async {
    Map<String, dynamic> data = {
      "item_id": itemId,
    };

    return await request(
      path: "/purchase/item/detail",
      data: data,
      method: Method.get,
      isShowLoad: isShowLoad,
      isShowError: true,
      policy: CachePolicy.forceCache,
    );
  }

  /// 创建采购订单
  /// [orderInfo] 订单信息
  /// [isShowLoad] 是否显示加载动画
  static Future createPurchaseOrder({
    required Map<String, dynamic> orderInfo,
    bool isShowLoad = true,
  }) async {
    return await request(
      path: "/purchase/order/create",
      data: orderInfo,
      method: Method.post,
      isShowLoad: isShowLoad,
      isShowError: true,
      isShowSuccessToast: true,
      policy: CachePolicy.noCache,
    );
  }

  /// 获取采购订单列表
  /// [page] 页码
  /// [status] 订单状态
  /// [isShowLoad] 是否显示加载动画
  static Future getPurchaseOrderList({
    int page = 1,
    int? status,
    bool isShowLoad = false,
  }) async {
    return await MedicalMockApis.getPurchaseOrderList(
      page: page,
      status: status,
    );
  }

  /// 获取地址列表
  /// [isShowLoad] 是否显示加载动画
  static Future getAddressList({
    bool isShowLoad = false,
  }) async {
    return await MedicalMockApis.getAddressList();
  }

  /// 保存地址
  /// [addressInfo] 地址信息
  /// [isShowLoad] 是否显示加载动画
  static Future saveAddress({
    required Map<String, dynamic> addressInfo,
    bool isShowLoad = true,
  }) async {
    return await request(
      path: "/address/save",
      data: addressInfo,
      method: Method.post,
      isShowLoad: isShowLoad,
      isShowError: true,
      isShowSuccessToast: true,
      policy: CachePolicy.noCache,
    );
  }

  /// 删除地址
  /// [addressId] 地址ID
  /// [isShowLoad] 是否显示加载动画
  static Future deleteAddress({
    required String addressId,
    bool isShowLoad = true,
  }) async {
    Map<String, dynamic> data = {
      "address_id": addressId,
    };

    return await request(
      path: "/address/delete",
      data: data,
      method: Method.post,
      isShowLoad: isShowLoad,
      isShowError: true,
      isShowSuccessToast: true,
      policy: CachePolicy.noCache,
    );
  }

  // ========== 通用接口 ========== //

  /// 上传文件
  /// [filePath] 文件路径
  /// [isShowLoad] 是否显示加载动画
  static Future uploadFile({
    required String filePath,
    bool isShowLoad = true,
  }) async {
    return await request(
      path: "/upload/file",
      data: {
        "file": filePath,
      },
      method: Method.post,
      isFormData: true,
      isShowLoad: isShowLoad,
      isShowError: true,
      policy: CachePolicy.noCache,
      options: Options(
        sendTimeout: const Duration(milliseconds: 30000),
      ),
    );
  }
}
